package com.test.springCloud.utils;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

import java.io.BufferedInputStream;
import java.io.IOException;

/**
 * ftp 上传文件
 * FTPClient commons.net 3.0.1版本
 * @author Lenovo
 *
 */
public class FtpUtils {

    private static final String hostname = "127.0.0.1";//FTP IP

    private static final int port = 21;//FTP 端口号

    private static final String username = "ftpName";//FTP 登录账号

    private static final String password = "ftpPsd"; //FTP 登录密码

    private static final String pathname = "/";//FTP 工作路径


    /**
     * 上传文件（可供Action/Controller层使用）
     * @param fileName 上传到FTP服务器后的文件名称
     * @param inputStream 输入文件流
     * @return
     */
    public static boolean uploadFile(String fileName, BufferedInputStream inputStream){

        boolean flag = false;
        FTPClient ftpClient = new FTPClient();
        //设置超时
        ftpClient.setConnectTimeout(60*60*1000);
        //设置编码
        ftpClient.setControlEncoding("UTF-8");
        try {
            //连接FTP服务器
            ftpClient.connect(hostname, port);
            //登录FTP服务器
            ftpClient.login(username, password);
            //是否成功登录FTP服务器
            int replyCode = ftpClient.getReplyCode();
            if(!FTPReply.isPositiveCompletion(replyCode)){
                return flag;
            }
            System.out.println("===========登录FTP成功了==========");
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            //切换路径 创建路径
            ftpClient.makeDirectory(pathname);
            ftpClient.changeWorkingDirectory(pathname);
            ftpClient.enterLocalPassiveMode();
            //设置缓冲
            ftpClient.setBufferSize(1024 * 1024 * 20);
            //保持连接
            ftpClient.setKeepAlive(true);
            boolean a = ftpClient.storeFile(new String(fileName.getBytes("utf-8"),"iso-8859-1"), inputStream);
            if(a){
                System.out.println("===========创建文件成功=============="+a);
                String fileName2 = fileName+"AAA";
                boolean status = ftpClient.rename(fileName, fileName2);
                if(status)
                    System.out.println("===========修改文件名称成功=============="+status);
            }
            inputStream.close();
            ftpClient.logout();
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            if(ftpClient.isConnected()){
                try {
                    ftpClient.disconnect();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return flag;

    }



    /* public static void main(String[] args) {
         String originfilename = "C:\\Users\\Lenovo\\Desktop\\xx.txt";
         FileInputStream inputStream;
         try {
            inputStream = new FileInputStream(new File(originfilename));
            boolean a = uploadFile("xx.txt","/104/",inputStream);
            System.out.println("上传文件成功============"+a);
         } catch (FileNotFoundException e) {
            e.printStackTrace();
         }
    }*/


}
